package cn.tedu.niaoge.controller;

import cn.tedu.niaoge.entity.Manager;
import cn.tedu.niaoge.mapper.ManagerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
public class ManagerController {

    @Autowired(required = false)
    ManagerMapper mapper;

    @RequestMapping("/login")
    public int login(Manager manager, String rem, HttpServletResponse response, HttpSession session){
        System.out.println("manager"+manager+",rem="+rem);
        Manager m=mapper.selectByManagername(manager.getManagername());
        if (m!=null){//用户存在
            if (manager.getPassword().equals(m.getPassword())){
                if (rem!=null){
                    Cookie c1=new Cookie("managername",m.getManagername());
                    Cookie c2=new Cookie("password",m.getPassword());
                    c2.setMaxAge(60*60*24*30);
                    response.addCookie(c1);
                    response.addCookie(c2);
                }
                session.setAttribute("manager",m);
                return 1;/*登录成功*/
            }else {
                return 2;/*密码错误*/
            }
        }
        return 0;/*用户名不存在*/
    }

    @RequestMapping("/currentManager")
    public Manager currentManager(HttpSession session){
        return (Manager) session.getAttribute("manager");
    }
}
